package com.robot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.common.api.CurrentUserSupport;
import com.robot.common.enums.StatsEnum;
import com.robot.common.result.PageResult;
import com.robot.common.result.Response;
import com.robot.dao.dto.IntegralOperateDTO;
import com.robot.dao.dto.LargeMarketDTO;
import com.robot.dao.dto.PageRobotStatsDTO;
import com.robot.dao.dto.StatsDTO;
import com.robot.dao.entity.Robot;
import com.robot.dao.mapper.ChatMapper;
import com.robot.dao.mapper.IntegralLogMapper;
import com.robot.dao.mapper.RobotMapper;
import com.robot.dao.mapper.UserMapper;
import com.robot.dao.vo.*;
import com.robot.service.IntegralLogService;
import com.robot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Statistics")
@Slf4j
@Api(tags = "大盘页相关接口")
public class StatisticsController {

    @Autowired
    private IntegralLogService integralLogService;

    @Autowired
    private CurrentUserSupport currentUserSupport;
    @Autowired
    private IntegralLogMapper integralLogMapper;
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RobotMapper robotMapper;
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/personstats")
    @ApiOperation(value = "统计7天使用人数")
    public Response personstats(@RequestBody StatsDTO statsDTO)
    {
        statsDTO.setUserId(String.valueOf(currentUserSupport.getCurrentUserId()));
        StatsVO statsVO = new StatsVO();
        if (statsDTO.getCompanyCode()!=null){
            List<String> userIdList=userMapper.findUsersByCompanyCode(statsDTO.getCompanyCode());
            Integer personnumber = chatMapper.countDistinctUsersByUserIds(userIdList);
            statsVO.setCompanyperson(String.valueOf(personnumber));
        }
        else {
            UserInfoVO userInfoVO = userService.geUserById(Integer.valueOf(statsDTO.getUserId()));
            List<String> userIdList=userMapper.findUsersByCompanyCode(userInfoVO.getCompanyCode());
            Integer personnumber = chatMapper.countDistinctUsersByUserIds(userIdList);
            statsVO.setCompanyperson(String.valueOf(personnumber));
        }
        return Response.success(statsVO);
    }

    @PostMapping("/chatstats")
    @ApiOperation(value = "统计7天对话次数")
    public Response chatstats(@RequestBody StatsDTO statsDTO)
    {
        StatsVO statsVO = new StatsVO();
        statsDTO.setUserId(String.valueOf(currentUserSupport.getCurrentUserId()));
        if(Integer.valueOf(statsDTO.getStatsType()).equals(StatsEnum.INDIVIDUAL.getCode())){
            Integer chatnumber = chatMapper.countChatHistoryByUserId(statsDTO);
            statsVO.setChatNumber(chatnumber);
        }else if (Integer.valueOf(statsDTO.getStatsType()).equals(StatsEnum.ENTERPRISE.getCode())){
            UserInfoVO userInfoVO = userService.geUserById(Integer.valueOf(statsDTO.getUserId()));
            List<String> userIdList=userMapper.findUsersByCompanyCode(userInfoVO.getCompanyCode());
            Integer chatnumber = chatMapper.countChatHistoryByUserIds(userIdList);
            statsVO.setChatNumber(chatnumber);
        }
        if (statsVO.getChatNumber() == null){
            return Response.error("查询不到");
        }
            return Response.success(statsVO);
        }

    @PostMapping("/robotstats")
    @ApiOperation(value = "智能体统计分页查询")
    public Response<PageResult> robotstats(@RequestBody StatsDTO statsDTO)
    {
        statsDTO.setUserId(String.valueOf(currentUserSupport.getCurrentUserId()));
//        statsDTO.setUserId(String.valueOf(41L));
        UserInfoVO userInfoVO = userService.geUserById(Integer.valueOf(statsDTO.getUserId()));
        PageHelper.startPage(statsDTO.getPage(), statsDTO.getPageSize());
        Page<PageRobotStatsDTO> pageResult = robotMapper.pageQueryStats(Long.valueOf(statsDTO.getUserId()), String.valueOf(userInfoVO.getCompanyCode()),statsDTO.getRobotName(),statsDTO.getRobotDescription());

        List<RobotStatsQueryVO> robotStatsQueryVOList = new ArrayList<>();

        List<PageRobotStatsDTO> robots = pageResult.getResult();
        for (PageRobotStatsDTO robot : robots) {
            RobotStatsQueryVO robotStatsQueryVO = new RobotStatsQueryVO();
            BeanUtils.copyProperties(robot, robotStatsQueryVO);
            robotStatsQueryVO.setId(robot.getId());
            robotStatsQueryVO.setRobotDescription(robot.getRobotDescription());
            robotStatsQueryVO.setRobotName(robot.getRobotName());
            robotStatsQueryVO.setViewNum(Long.valueOf(robot.getViewTimes()));
            robotStatsQueryVO.setAnswerNum(chatMapper.countChatHistoryByRobotId(robot.getId()));
            robotStatsQueryVO.setPersonNum(Long.valueOf(chatMapper.countDistinctUsersByRobotId(String.valueOf(robot.getId()))));
            Long consume = integralLogMapper.sumIntegralLogByRobotId(robot.getId());
            if (consume != null){
                robotStatsQueryVO.setIntegralConsumption(consume);
            }else {
                robotStatsQueryVO.setIntegralConsumption(0L);
            }

            robotStatsQueryVOList.add(robotStatsQueryVO);
        }
        boolean hasMore = statsDTO.getPage() < pageResult.getPages();
        return Response.success(new PageResult(pageResult.getTotal(),hasMore,robotStatsQueryVOList));
    }




    @PostMapping("/sevenDaysIntegralUsage")
    @ApiOperation("7天积分用量")
    public Response<String> integralUsage(@RequestBody LargeMarketDTO largeMarketDto) {
       Long score= integralLogService.integralUsage(largeMarketDto);
       return Response.success(score.toString());
    }



    @PostMapping("/integralTrend")
    @ApiOperation("平台积分用量趋势图表")
    public Response<LargeMarketVO> integralTrend(@RequestBody LargeMarketDTO largeMarketDTO) throws ParseException {
        LargeMarketVO largeMarketVO=integralLogService.integralTrend(largeMarketDTO);
        return Response.success(largeMarketVO);
    }


    @PostMapping("/conversationsNum")
    @ApiOperation("对话次数图表")
    public Response<LargeMarketVO> integralFrequency(@RequestBody LargeMarketDTO largeMarketDTO) throws ParseException {
        LargeMarketVO largeMarketVO=integralLogService.integralFrequency(largeMarketDTO);
        return Response.success(largeMarketVO);
    }

}
